package com.zwz.crawler.main;

import com.zwz.constant.Constant;
import com.zwz.parse.JsonParser;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zwz.constant.Constant.MILLIS;


/**
 * 企业主信息查询
 */
@Slf4j
public class TungeeLeadsQuery {

    private static int total;

    private static final int PAGE_SIZE = 50;

    String baseUrl = "https://sales.tungee.com/api/unlock-leads-call";
    static  String BASE_URL = "https://sales.tungee.com/api/unlock-leads-call";

//     static String queryParams = "sort_field=create_time&sort=-1&begin="+begin+"&end="+end;

//    public static void main(String[] args) {
////        startTungeeLeadsQuery();
//    }
    public static List<String> startTungeeLeadsQuery(String fullUrl ) {
        // 1. 构建带参数的URL（GET请求参数直接拼接在URL中）

//        String queryParams = "sort_field=create_time&sort=-1&begin=0&end=50";
//        String queryParams = "sort_field=create_time&sort=-1&begin="+begin+"&end="+end;
//        String fullUrl = baseUrl + "?" + queryParams;
        List<String> res=new ArrayList<>();
        try {
            // 2. 发送GET请求
            Connection.Response response = Jsoup.connect(fullUrl)
                    .method(Connection.Method.GET)
                    // 3. 设置请求头（关键头信息）
                    .header("authority", "sales.tungee.com")
                    .header("scheme", "https")
                    .header("accept", "*/*")
                    .header("accept-encoding", "gzip, deflate, br") // 注意：实际可能需要关闭自动压缩
                    .header("accept-language", "zh-CN,zh;q=0.9")
                    .header("cache-control", "no-cache")
                    .header("pragma", "no-cache")
                    .header("referer", "https://sales.tungee.com/unlock-list/enterprise")
                    .header("sec-ch-ua", "\"Not_A Brand\";v=\"99\", \"Google Chrome\";v=\"109\", \"Chromium\";v=\"109\"")
                    .header("sec-ch-ua-mobile", "?0")
                    .header("sec-ch-ua-platform", "\"Windows\"")
                    .header("sec-fetch-dest", "empty")
                    .header("sec-fetch-mode", "cors")
                    .header("sec-fetch-site", "same-origin")
                    .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36")
                    .header("x-tonxis-pid", "67864ea75f69e05d3b544ca7")
                    .header("x-tonxis-sid", "F0UiGSEUkGeGTqdfaeBdO1RMp4pLNUVBWf68YIF25rVjA0Y=")
                    .header("x-tonxis-signature", "DlQuRX2nEhsVRXsVRXsrEhEV8C1s84NrMJqVMzjrEh2rMztzRPMKRGMOMXfJM4Qz842uLU5WQo52.IsdoffC6HAqe0+mYtePCHKo8r1DVZGv.xZ1W98X9yEFW4sSwEGHQZHCSYJk0tMakB2KFCw0epS7EuQOYJldtTKs.9biPDCpXHLn4LeDj6gLvQ5cNCuYacyTSMYx6gyhCvFwl7UvUQAn12p9Wprix8Yl7RD4LNdZSgj6f62A5IACGLp4Sx33")
                    // 4. 设置完整的Cookie（从浏览器复制最新值）
                 //   .header("cookie", "_tx_pid=67864ea75f69e05d3b544ca7; __wpkreporterwid_=1e252b09-1579-4065-baa9-259171f3cd56; __last_enter_version=sales; _tx_cid=6805a5576eceb9637696f09d; DISTINCT_ID=b2ff747e-4b39-42fe-aece-249d6b8a3d3f; _tx_sid=MTc0NTIyMTkyMTE0OTo4YTRiMzU0NTQxNTlmZWJjNjA4MTc2ZTZiNTYzMDM0Ng==; Hm_lvt_f2ee75449fc055cc4dbceb4fe403bea3=1745221661,1745225090,1745227144,1745228061; HMACCOUNT=676A9C43EBB88158; Hm_lvt_d3ee3af8af62f47558a292277356e90f=1744947754,1745221273,1745290315; HMACCOUNT=676A9C43EBB88158; Qs_lvt_309705=1744956213%2C1744956391%2C1744956490%2C1745221273%2C1745290314; Hm_lpvt_d3ee3af8af62f47558a292277356e90f=1745290347; _uetsid=00dcdb601e8411f08e8985a5cb4ab811; _uetvid=2a1706601c0711f0be3333b35f4687c7; Qs_pv_309705=635693979573886800%2C1483374110121918000%2C2291643818348700700%2C1514669150938964200%2C2262502280575317000; SecurityCenterDuId=ImFBSG15RzdPdVdPdCtsWnNGMFNTWTVFPSI.GuiWdA.3Cs56Qgr5dGe4ntXXtXtYI9OA28; remember_token=680704f46eceb963b91dda0a|43cf5f15c14273d06f694595a24e6d31c4b6f83a2b44d46dd7cbe6bc5b1507d42bda4b1bbae32d570e00114499e0581c5a1fc82593d8b534a67c8133f1d8164a; _co_i=6805a5576eceb9637696f09d; _tx_uid=680701dddca941522b77e864; tg_referrer_source=https%3A%2F%2Finbound.tungee.com%2F; acw_tc=0a47314f17452926495232178e005809c93a76f11e3c512ac7828586a6e57d; Hm_lpvt_f2ee75449fc055cc4dbceb4fe403bea3=1745292654; accountCenterSessionId=.eJwtjs1OwzAQhN_F5x42Try2e0biUgdVikDkEq3ttZrGCVKSAiri3TGIy2gO8_N9idvG6zBGcRRoQEOTGuTA3mLtbRUjAYmDWJjjsNE7D_vbEL04JsobH0S40LJwLuUP9iX3PxR8owkJGaLhWqZkQRUxhJVNNqA3ADWgUbL8JdtIkxJBqItVrKUlwqaWIWpUiqEUKETjFXogFdgwGx0tRVAmmOg1S0rofck3kU2FvyBp5e0ijvt6K5jbH9ZrN326a57dNezunsd2BHD35-nUTVX_eN7d3GfX9fPTQ5vblzOI7x9MSVeX.Guie8Q.OwnciSp6K2F03-Gz9CqaHXHjmDg; doncusSessionId=eyJfZnJlc2giOmZhbHNlLCJ1c2VyX2lkIjoiNjgwNWE1NTc2ZWNlYjk2Mzc2OTZmMGEyIn0.Guie7g.HNNV0UAzNedtC-zDgKAIPoM4bz0")
//                    .header("cookie","acw_tc=ac11000117453016495175273e0082d2f993ab593002a01c5138cb34500d66; _tx_pid=67864ea75f69e05d3b544ca7; __wpkreporterwid_=dc6dbcf2-6e65-46c3-0632-1632ef91f99d; __last_enter_version=sales; Hm_lvt_f2ee75449fc055cc4dbceb4fe403bea3=1745301650; HMACCOUNT=98C3A46A1E528DCB; SecurityCenterDuId=ImFBSG15RzdPdVdPdCtsWnNGMFVuS0dVPSI.GujCIQ.kRe48kn7O9EWyCFmTjVSSlZRzYE; _co_i=6805a5576eceb9637696f09d; _tx_uid=680701dddca941522b77e864; _tx_cid=6805a5576eceb9637696f09d; DISTINCT_ID=9ebeafa0-ddee-4b93-bd55-014d9b7826df; _tx_sid=MTc0NTMwMTY3MjczNDo4MmQ2OTQ5MTY0NjU2ODNmYjQ5MjlmMjc3NDg5ZDFlZg==; tg_referrer_source=https%3A%2F%2Fuser.tungee.com%2F; Hm_lpvt_f2ee75449fc055cc4dbceb4fe403bea3=1745301725; accountCenterSessionId=.eJwlj01LxEAQRP_LnPfQMz1fm3tcBEdRFiReQs90D3HJRkiyLq743w16q4LHo-pb9e-sGoWEsUatMwiyR666Rq9Fs2TJNeggSLGYzGDJirWllBAIAQxQgT0AhBqRY7REVmPISJVcCdYUB0iZrQPW2YONYLzVbMWAqYKbmQOK2qlJhPuFPqVfP3rOqqk0LrJTfZ1lGVSzzpetLX9znw7ttTu9nNK5XdMh3dIXwOOxhYfXe9fdnte3LSdzd07HcehMMpv-ssj8_9VHCAikvRTJe4-eisOKboPKQNMk4wZdJaufX4X8VIY.GujCXg.0TJiFF3BnI84ny-3hNjvnTevmFM")
                    .header("cookie", Constant.cookies)
                    .ignoreContentType(true)  // 允许读取JSON响应
                    .ignoreHttpErrors(true)   // 避免直接抛出HTTP错误异常
                    .execute();

            // 5. 输出结果
           log.info("HTTP状态码: " + response.statusCode());
           log.info("响应内容: \n" + response.body());

            // 6. 处理401未授权错误
            if (response.statusCode() == 401) {
                System.err.println("错误: 授权失效，请检查以下内容：");
                System.err.println("1. Cookie是否过期（需重新登录获取）");
                System.err.println("2. x-tonxis-signature/x-tonxis-sid是否需要动态生成");
            }

            //7.解析总的记录数




             res = JsonParser.parse(response.body());


//              total = JsonParser.parseCount(response.body());
//
//            while( ){
//
//                int PageSize=50;
//                startTungeeLeadsQuery(50,50*PageSize);
//            }






        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }




    public static int  getTotal(String fullUrl ) {
        // 1. 构建带参数的URL（GET请求参数直接拼接在URL中）

//        String queryParams = "sort_field=create_time&sort=-1&begin=0&end=50";
//        String queryParams = "sort_field=create_time&sort=-1&begin="+begin+"&end="+end;
//        String fullUrl = baseUrl + "?" + queryParams;
        List<String> res=new ArrayList<>();
        try {
            // 2. 发送GET请求
            Connection.Response response = Jsoup.connect(fullUrl)
                    .method(Connection.Method.GET)
                    // 3. 设置请求头（关键头信息）
                    .header("authority", "sales.tungee.com")
                    .header("scheme", "https")
                    .header("accept", "*/*")
                    .header("accept-encoding", "gzip, deflate, br") // 注意：实际可能需要关闭自动压缩
                    .header("accept-language", "zh-CN,zh;q=0.9")
                    .header("cache-control", "no-cache")
                    .header("pragma", "no-cache")
                    .header("referer", "https://sales.tungee.com/unlock-list/enterprise")
                    .header("sec-ch-ua", "\"Not_A Brand\";v=\"99\", \"Google Chrome\";v=\"109\", \"Chromium\";v=\"109\"")
                    .header("sec-ch-ua-mobile", "?0")
                    .header("sec-ch-ua-platform", "\"Windows\"")
                    .header("sec-fetch-dest", "empty")
                    .header("sec-fetch-mode", "cors")
                    .header("sec-fetch-site", "same-origin")
                    .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36")
                    .header("x-tonxis-pid", "67864ea75f69e05d3b544ca7")
                    .header("x-tonxis-sid", "F0UiGSEUkGeGTqdfaeBdO1RMp4pLNUVBWf68YIF25rVjA0Y=")
                    .header("x-tonxis-signature", "DlQuRX2nEhsVRXsVRXsrEhEV8C1s84NrMJqVMzjrEh2rMztzRPMKRGMOMXfJM4Qz842uLU5WQo52.IsdoffC6HAqe0+mYtePCHKo8r1DVZGv.xZ1W98X9yEFW4sSwEGHQZHCSYJk0tMakB2KFCw0epS7EuQOYJldtTKs.9biPDCpXHLn4LeDj6gLvQ5cNCuYacyTSMYx6gyhCvFwl7UvUQAn12p9Wprix8Yl7RD4LNdZSgj6f62A5IACGLp4Sx33")
                    // 4. 设置完整的Cookie（从浏览器复制最新值）
                    //   .header("cookie", "_tx_pid=67864ea75f69e05d3b544ca7; __wpkreporterwid_=1e252b09-1579-4065-baa9-259171f3cd56; __last_enter_version=sales; _tx_cid=6805a5576eceb9637696f09d; DISTINCT_ID=b2ff747e-4b39-42fe-aece-249d6b8a3d3f; _tx_sid=MTc0NTIyMTkyMTE0OTo4YTRiMzU0NTQxNTlmZWJjNjA4MTc2ZTZiNTYzMDM0Ng==; Hm_lvt_f2ee75449fc055cc4dbceb4fe403bea3=1745221661,1745225090,1745227144,1745228061; HMACCOUNT=676A9C43EBB88158; Hm_lvt_d3ee3af8af62f47558a292277356e90f=1744947754,1745221273,1745290315; HMACCOUNT=676A9C43EBB88158; Qs_lvt_309705=1744956213%2C1744956391%2C1744956490%2C1745221273%2C1745290314; Hm_lpvt_d3ee3af8af62f47558a292277356e90f=1745290347; _uetsid=00dcdb601e8411f08e8985a5cb4ab811; _uetvid=2a1706601c0711f0be3333b35f4687c7; Qs_pv_309705=635693979573886800%2C1483374110121918000%2C2291643818348700700%2C1514669150938964200%2C2262502280575317000; SecurityCenterDuId=ImFBSG15RzdPdVdPdCtsWnNGMFNTWTVFPSI.GuiWdA.3Cs56Qgr5dGe4ntXXtXtYI9OA28; remember_token=680704f46eceb963b91dda0a|43cf5f15c14273d06f694595a24e6d31c4b6f83a2b44d46dd7cbe6bc5b1507d42bda4b1bbae32d570e00114499e0581c5a1fc82593d8b534a67c8133f1d8164a; _co_i=6805a5576eceb9637696f09d; _tx_uid=680701dddca941522b77e864; tg_referrer_source=https%3A%2F%2Finbound.tungee.com%2F; acw_tc=0a47314f17452926495232178e005809c93a76f11e3c512ac7828586a6e57d; Hm_lpvt_f2ee75449fc055cc4dbceb4fe403bea3=1745292654; accountCenterSessionId=.eJwtjs1OwzAQhN_F5x42Try2e0biUgdVikDkEq3ttZrGCVKSAiri3TGIy2gO8_N9idvG6zBGcRRoQEOTGuTA3mLtbRUjAYmDWJjjsNE7D_vbEL04JsobH0S40LJwLuUP9iX3PxR8owkJGaLhWqZkQRUxhJVNNqA3ADWgUbL8JdtIkxJBqItVrKUlwqaWIWpUiqEUKETjFXogFdgwGx0tRVAmmOg1S0rofck3kU2FvyBp5e0ijvt6K5jbH9ZrN326a57dNezunsd2BHD35-nUTVX_eN7d3GfX9fPTQ5vblzOI7x9MSVeX.Guie8Q.OwnciSp6K2F03-Gz9CqaHXHjmDg; doncusSessionId=eyJfZnJlc2giOmZhbHNlLCJ1c2VyX2lkIjoiNjgwNWE1NTc2ZWNlYjk2Mzc2OTZmMGEyIn0.Guie7g.HNNV0UAzNedtC-zDgKAIPoM4bz0")
//                    .header("cookie","acw_tc=ac11000117453016495175273e0082d2f993ab593002a01c5138cb34500d66; _tx_pid=67864ea75f69e05d3b544ca7; __wpkreporterwid_=dc6dbcf2-6e65-46c3-0632-1632ef91f99d; __last_enter_version=sales; Hm_lvt_f2ee75449fc055cc4dbceb4fe403bea3=1745301650; HMACCOUNT=98C3A46A1E528DCB; SecurityCenterDuId=ImFBSG15RzdPdVdPdCtsWnNGMFVuS0dVPSI.GujCIQ.kRe48kn7O9EWyCFmTjVSSlZRzYE; _co_i=6805a5576eceb9637696f09d; _tx_uid=680701dddca941522b77e864; _tx_cid=6805a5576eceb9637696f09d; DISTINCT_ID=9ebeafa0-ddee-4b93-bd55-014d9b7826df; _tx_sid=MTc0NTMwMTY3MjczNDo4MmQ2OTQ5MTY0NjU2ODNmYjQ5MjlmMjc3NDg5ZDFlZg==; tg_referrer_source=https%3A%2F%2Fuser.tungee.com%2F; Hm_lpvt_f2ee75449fc055cc4dbceb4fe403bea3=1745301725; accountCenterSessionId=.eJwlj01LxEAQRP_LnPfQMz1fm3tcBEdRFiReQs90D3HJRkiyLq743w16q4LHo-pb9e-sGoWEsUatMwiyR666Rq9Fs2TJNeggSLGYzGDJirWllBAIAQxQgT0AhBqRY7REVmPISJVcCdYUB0iZrQPW2YONYLzVbMWAqYKbmQOK2qlJhPuFPqVfP3rOqqk0LrJTfZ1lGVSzzpetLX9znw7ttTu9nNK5XdMh3dIXwOOxhYfXe9fdnte3LSdzd07HcehMMpv-ssj8_9VHCAikvRTJe4-eisOKboPKQNMk4wZdJaufX4X8VIY.GujCXg.0TJiFF3BnI84ny-3hNjvnTevmFM")
                    .header("cookie", Constant.cookies)
                    .ignoreContentType(true)  // 允许读取JSON响应
                    .ignoreHttpErrors(true)   // 避免直接抛出HTTP错误异常
                    .execute();

            // 5. 输出结果
            log.info("HTTP状态码: " + response.statusCode());
            log.info("响应内容: \n" + response.body());

            // 6. 处理401未授权错误
            if (response.statusCode() == 401) {
                System.err.println("错误: 授权失效，请检查以下内容：");
                System.err.println("1. Cookie是否过期（需重新登录获取）");
                System.err.println("2. x-tonxis-signature/x-tonxis-sid是否需要动态生成");
            }

            //7.解析总的记录数




            res = JsonParser.parse(response.body());


            total = JsonParser.parseCount(response.body());

//            while( ){
//
//                int PageSize=50;
//                startTungeeLeadsQuery(50,50*PageSize);
//            }






        } catch (IOException e) {
            e.printStackTrace();
        }
        return total;
    }




    /**
     *    //这里从网页直接获取不到，这样根据总的记录数不断加载
     * @param args
     * @return
     * @throws InterruptedException
     */
    public static List<String> getTotalRecotd() throws InterruptedException {
        String queryParams = "sort_field=create_time&sort=-1&begin=0&end=50";
        // 1. 获取总记录数
        String url = "https://sales.tungee.com/api/unlock-leads-call" + "?" + queryParams;
        int totalRecords = getTotal(url);
        System.out.println("Total records: " + totalRecords);

        HashMap map=new HashMap();

        // 2. 分页爬取数据
        List<String> pageData = null;
        List<String> pageDatas = new ArrayList<>();

        StringBuilder sb = new StringBuilder();
//        for (List<String> list : allLists) {
//            for (String s : list) {
//                sb.append(s);
//            }
//        }
        String result = sb.toString();


        for (int begin = 0; begin < totalRecords; begin += PAGE_SIZE) {
            int end = Math.min(begin + PAGE_SIZE - 1, totalRecords - 1);
            url = buildUrl(begin, end + 1);

            System.out.println("Fetching: " + url);

            // 发送请求并处理响应
            pageData = startTungeeLeadsQuery(url);
            if (pageData.isEmpty()) {
                break;
            }

            // 处理数据（示例：打印记录数）
            System.out.println("Got " + pageData.size() + " items");



            //将Listcopy 到 中
//            String value=null;
//            for( int k=0;k<pageData.size();k++){
//                  value = pageData.get(k);
//                  sb.append(value);
//            }

            pageDatas.addAll(pageData);

            // 请求间隔（避免封禁）
            Thread.sleep(MILLIS);

        }
        //返回总的记录数即可
        return pageDatas;

    }



    private static String buildUrl(int begin, int end) {
        return BASE_URL + "?sort_field=create_time&sort=-1&begin=" + begin + "&end=" + end;
    }


    }

